package it.tava.andbudget.data.parser;

import it.tava.andbudget.data.resource.Charge;
import it.tava.andbudget.data.resource.ResourceManager;
import it.tava.andbudget.data.resource.XmlResourceManager;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.res.XmlResourceParser;
import android.util.Log;

public class ResourceParser implements ParserInterface {

	private XmlResourceParser parser;
	private ResourceManager manager;

	public ResourceParser(XmlPullParser parser) {
		this.parser = (XmlResourceParser) parser;
		this.manager = new XmlResourceManager();
	}

	public ResourceManager parse() {
		/*try {
			doParsing();
		} catch (XmlPullParserException e) {
			Log.e("", e.getMessage());
		} catch (IOException e) {
			Log.e("", e.getMessage());
		}*/

		return manager;
	}

/*	private void doParsing() throws XmlPullParserException, IOException {
		initParsing();

		while(parser.getEventType() != XmlResourceParser.END_DOCUMENT) {
			switch(parser.getEventType()) {
			
			case XmlResourceParser.START_TAG:
				if(parser.getName().equals(XmlResourceManager.BUDGET_NODE)) {
					manager.addBudget();
				}
				else if(parser.getName().equals(XmlResourceManager.YEAR_NODE)) {
					if(parser.getAttributeCount() < 1) {
						throw new XmlPullParserException("Year tag: invalid attribute count");
					}
					
					int id = parser.getAttributeIntValue(0, 0);
					
					if(id < 1900) {
						throw new XmlPullParserException("Year tag: invalid attribute id value");
					}
					
					manager.addYear(new Year(id));
				}
				else if(parser.getName().equals(XmlResourceManager.MONTH_NODE)) {
					if(parser.getAttributeCount() < 1) {
						throw new XmlPullParserException("Month tag: invalid attribute count");
					}
					
					int id = parser.getAttributeIntValue(0, 0);
					
					if(id < 1 || id > 12) {
						throw new XmlPullParserException("Month tag: invalid attribute id value");
					}
					
					manager.addMonth(new Month(id));
				}
				else if(parser.getName().equals(XmlResourceManager.DAY_NODE)) {
					if(parser.getAttributeCount() < 1) {
						throw new XmlPullParserException("Day tag: invalid attribute count");
					}
					
					int id = parser.getAttributeIntValue(0, 0);
					
					if(id < 1 || id > 31) {
						throw new XmlPullParserException("Day tag: invalid attribute id value");
					}
					
					manager.addDay(new Day(id));
				}
				else if(parser.getName().equals(XmlResourceManager.CHARGE_NODE)) {
					if(parser.getAttributeCount() < 2) {
						throw new XmlPullParserException("Cost tag: invalid attribute count");
					}
					
					int id = parser.getAttributeIntValue(0, 0);
					String category = parser.getAttributeValue(1);
					String description;
					float cost;
					
					parser.require(XmlResourceParser.START_TAG, "", XmlResourceManager.DESCRIPTION_NODE);
					description = parser.nextText();
					parser.require(XmlResourceParser.END_TAG, "", XmlResourceManager.DESCRIPTION_NODE);
					
					parser.require(XmlResourceParser.START_TAG, "", XmlResourceManager.COST_NODE);
					cost = Float.valueOf(parser.nextText());
					parser.require(XmlResourceParser.END_TAG, "", XmlResourceManager.COST_NODE);
					
					manager.addCharge(new Charge(id, category, description, cost));
				}
				
				break;
				
			case XmlResourceParser.END_TAG:
				if(parser.getName().equals(XmlResourceManager.BUDGET_NODE)) {
					manager.endBudget();
				}
				else if(parser.getName().equals(XmlResourceManager.YEAR_NODE)) {
					manager.endYear();
				}
				else if(parser.getName().equals(XmlResourceManager.MONTH_NODE)) {
					manager.endMonth();
				}
				else if(parser.getName().equals(XmlResourceManager.DAY_NODE)) {
					manager.endDay();
				}
				else if(parser.getName().equals(XmlResourceManager.COST_NODE)) {
					manager.endCharge();
				}
				
				break;
			}
			
			parser.next();
		}
	}*/

	private void initParsing() throws XmlPullParserException, IOException {
		while(parser.getEventType() != XmlResourceParser.START_TAG) {
			parser.next();
		}
	}
}
